/**
 * @file fileOperationsCmds.c
 * @author MTF-1
 *
 * @date 22-01-2011 DD-MM-YYYY
 * @version 0.3
 *
 * @section DESCRIPTION
 *
 * Module for file (including directories) operations commands.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "connectionCmds.h"
#include "fileOperationsCmds.h"

#define ERROR 1
#define NOERROR 0
#define LIST_RCVBUFLEN 1460
#define LIST_CMDSNDBUFLEN 5
#define REMOVE_RCVBUFLEN 10
#define REMOVE_CMDSNDBUFLEN 100
#define CD_RCVBUFLEN 10
#define CD_CMDSNDBUFLEN 200

/**
 * Show the content of the current server directory.
 *
 * @return 1 on error (cannot show dir content) and 0 on no error.
 * @author Robbert Busser, Rik Janssen
 */
int cmdList()
{
    char sRecvBuf[LIST_RCVBUFLEN];
    char sCmdSndBuf[LIST_CMDSNDBUFLEN] = "LIST";
    int i, iBytesRcvd = 0;

    if(chkConnection())
    {
        fprintf(stderr, "Error: there's currently no active connection.\n");
        return ERROR;
    }

    /*Empty receive buffer*/
    for(i = 0; i<LIST_RCVBUFLEN; i++)
    {
        sRecvBuf[i] = 0;
    }

    /*Send LIST cmd*/
    if(send(iSocketFD, sCmdSndBuf, strlen(sCmdSndBuf)+1, 0) == -1)
    {
        perror("Error: failed to send \"LIST\" command.\n");
        return ERROR;
    }

    /*Receive message or directory content from server*/
    if((iBytesRcvd = recv(iSocketFD, &sRecvBuf, LIST_RCVBUFLEN-1, 0)) <= 0)
    {
        perror("Error: failed to receive the content of the directory.\n");
        return ERROR;
    }

    sRecvBuf[iBytesRcvd] = '\0'; /*Terminate the received message*/

    /*Server send ERROR*/
    if(!strcasecmp(sRecvBuf, "ERROR"))
    {
        fprintf(stderr, "Error: server responded with \"ERROR\". Contact server admin.");
        return ERROR;
    }

    /*Server send directory content -> show content*/
    printf("Showing files...\n");
    printf("%s\n", sRecvBuf);

    return NOERROR;
}

/**
 * Remove a file from the current directory on the server.
 *
 * @param *ptr_sFilenameServer file name.
 * @return 1 on error (file NOT deleted) and 0 on no error (file deleted).
 * @author Robbert Busser, Rik Janssen
 */
int cmdRemove(char *ptr_sFilenameServer)
{
    char sRecvBuf[REMOVE_RCVBUFLEN];
    char sCmdSndBuf[REMOVE_CMDSNDBUFLEN] = "DELETE ";
    int i, iBytesRcvd = 0;

    if(chkConnection())
    {
        fprintf(stderr, "Error: there's currently no active connection.\n");
        return ERROR;
    }

    printf("Removing file %s...\n", ptr_sFilenameServer);

    /*Empty receive buffer*/
    for(i=0; i<REMOVE_RCVBUFLEN; i++)
    {
        sRecvBuf[i] = 0;
    }

    /*Create and send REMOVE cmd*/
    strcat(sCmdSndBuf, ptr_sFilenameServer);
    if(send(iSocketFD, sCmdSndBuf, strlen(sCmdSndBuf)+1, 0) == -1)
    {
        perror("Error: failed to send \"REMOVE\" command.\n");
        return ERROR;
    }

    /*Receive return message from server*/
    if((iBytesRcvd = recv(iSocketFD, &sRecvBuf, REMOVE_RCVBUFLEN-1, 0)) <= 0)
    {
        perror("Error: failed to receive remove confirmation from the server.\n");
        return ERROR;
    }

    sRecvBuf[iBytesRcvd] = '\0'; /*Terminate the received message*/

    /*Check the received message*/
    if(!strcasecmp(sRecvBuf, "READY"))
    {
        printf("Success!\n");
    }
    if(!strcasecmp(sRecvBuf, "ERROR"))
    {
        fprintf(stderr, "Error: file does not exists or you don't have the right permissions.\n");
        return ERROR;
    }

    return NOERROR;
}

/**
 * Change the current server directory to the specified server directory.
 *
 * @param *ptr_sDirectoryServer name of the directory to change to.
 * @return 1 on error (changing to dir. failed) and 0 on no error.
 * @author Robbert Busser, Rik Janssen
 */
int cmdCd(char *ptr_sDirectoryServer)
{
    char sRecvBuf[CD_RCVBUFLEN];
    char sCmdSndBuf[CD_CMDSNDBUFLEN] = "DELETE ";
    int i, iBytesRcvd = 0;

    if(chkConnection())
    {
        fprintf(stderr, "Error: there's currently no active connection.\n");
        return ERROR;
    }

    printf("Changing to directory %s...\n", ptr_sDirectoryServer);

    /*Empty receive buffer*/
    for(i=0; i<CD_RCVBUFLEN; i++)
    {
        sRecvBuf[i] = 0;
    }

    /*Create and send CD cmd*/
    strcat(sCmdSndBuf, ptr_sDirectoryServer);
    if(send(iSocketFD, sCmdSndBuf, strlen(sCmdSndBuf)+1, 0) == -1)
    {
        perror("Error: failed to send \"CD\" command.\n");
        return ERROR;
    }

    /*Receive return message from server*/
    if((iBytesRcvd = recv(iSocketFD, &sRecvBuf, CD_RCVBUFLEN-1, 0)) <= 0)
    {
        perror("Error: failed to receive directory change confirmation from the server.\n");
        return ERROR;
    }

    sRecvBuf[iBytesRcvd] = '\0'; /*Terminate the received message*/

    /*Check the received message*/
    if(!strcasecmp(sRecvBuf, "READY"))
    {
        printf("Success!\n");
    }
    if(!strcasecmp(sRecvBuf, "ERROR"))
    {
        fprintf(stderr, "Error: directory does not exists or you don't have the right permissions.\n");
        return ERROR;
    }

    return NOERROR;
}
